/*
 @description: 管理员登录

 @author: 李建翰

 @update: 2021年9月6日17:21:27
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")
class TokenController extends Controller{
    async index() {
        const { ctx, app } = this;
        const { ador_name, ador_poss } = ctx.request.body;
        // let user;
        // let pwd;
        // let jud;
        // let data;
        if(ador_name && ador_poss){
            const user=await app.mysql.query(`select ador_name from fuxiao_administrators`)
            const result=user.find(item=>item.ador_name===ador_name);
            if(result){
                let pwd=await app.mysql.query(`select ador_poss from fuxiao_administrators 
                where ador_name='${result.ador_name}'`)
                if(pwd[0].ador_poss===utility.md5(ador_poss)){
                    const tube1 = await app.mysql.query(`select judge from fuxiao_administrators where ador_name='${result.ador_name}'`);
                    let tube;
                    if(tube1[0].judge === 1){
                        tube = 1;
                    }else{
                        tube = 0;
                    }
                    const adminInfo={
                        ador_name,
                        ador_poss,
                        // tube,
                        login_time:Date.now()/1000,
                        end_time:Date.now()/1000*3600*2
                    };
                    const token = app.jwt.sign(adminInfo,app.config.jwt.sercet)
                    ctx.body={
                        code:0,
                        msg:"登录成功",
                        token,
                        judge:tube,
                        adminInfo
                    }
                
            } else {
                ctx.body = {
                    code: 4004,
                    msg: '账号或密码错误'
                }

            }
        }else{
            ctx.body={
                code:4000,
                msg:'格式不正确'
            }
        }
    }
    }
}
module.exports = TokenController